package com.example.blood_sugar.fragments;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
//import com.example.blood_sugar.view.Li

import com.example.blood_sugar.R;
import com.example.blood_sugar.bean.BSRecord;
import com.example.blood_sugar.db.BSRecordHolder;
import com.example.blood_sugar.view.LineGraphicView;

import java.util.ArrayList;

/**
 * 和BloodPressureChartFragment差不多，不多做注释
 */
public class BloodSugarChartFragment24Hours extends BaseFragment {

    ArrayList<Double> yList;
    ArrayList<String> xRawDatas;
    LineGraphicView mLineGraphicView;

    private BSRecord mBSRecord;

    public static BloodSugarChartFragment24Hours newInstance(String text){
        Bundle bundle = new Bundle();
        bundle.putString("text",text);
        BloodSugarChartFragment24Hours bloodSugarFragment7days = new BloodSugarChartFragment24Hours();
        bloodSugarFragment7days.setArguments(bundle);
        return  bloodSugarFragment7days;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_blood_sugar_chart_24hours, container, false);
        mLineGraphicView = view.findViewById(R.id.line_graphic);
        yList = new ArrayList<>();
        xRawDatas = new ArrayList<>();
        xRawDatas.add("凌晨");
        xRawDatas.add("早餐前");
        xRawDatas.add("早餐后");
        xRawDatas.add("午餐前");
        xRawDatas.add("午餐后");
        xRawDatas.add("晚餐前");
        xRawDatas.add("晚餐后");
        xRawDatas.add("睡前");
        initView();

        return view;
    }

    public void initView(){
        if (yList == null)
            return;
        mBSRecord = BSRecordHolder.get(getContext()).getLastBSRecord();
        if (yList.size() > 0){
            yList.clear();
        }
        yList.add(mBSRecord.getDawn());
        yList.add(mBSRecord.getBeforeBreakfast());
        yList.add(mBSRecord.getAfterBreakfast());
        yList.add(mBSRecord.getBeforeLunch());
        yList.add(mBSRecord.getAfterLunch());
        yList.add(mBSRecord.getBeforeDinner());
        yList.add(mBSRecord.getAfterDinner());
        yList.add(mBSRecord.getBeforeSleep());
        mLineGraphicView.setData(yList, xRawDatas, 18, 2);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // TODO: 2018/8/21 创建成功后初始化操作？
    }

/*    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (yList != null && isVisibleToUser) {
            //相当于Fragment的onResume
//            initView();
        } else {
            //相当于Fragment的onPause
        }
        Log.d("setUserVisibleHint", "setUserVisibleHint: ");
    }*/

/*    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.d("onSaveInstanceState", "onSaveInstanceState: ");
    }*/

/*    @Override
    public void onPause() {
        super.onPause();
        Log.d("onPause", "onPause: ");
    }*/

    @Override
    public void onResume() {
        super.onResume();
        initView();
    }
}
